package org.xdq.demo.apiadvice;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.xdq.demo.ex.BusinessException;
import org.xdq.demo.vo.R;

//@RestControllerAdvice声明本类对象是受spring管理的bean，并且是一个针对RestController的通知（切面功能）
//即本类将拦截所有注解为@RestController的控制器方法发生异常的情形
@RestControllerAdvice
@Slf4j
public class ApiAdvice {

    //private static final Logger log = LogManager.getLogger(ApiAdvice.class);

    //private static final Logger log = LoggerFactory.getLogger(ApiAdvice.class);

    public ApiAdvice(){
        log.debug("--------------------------{}对象创建了！",this.getClass().getName());
    }

    //针对发生BusinessException的Controller方法的处理,方法的返回结果将代替Controller方法的返回结果
    @ExceptionHandler(BusinessException.class)
    public R<?> handleBusinessException(BusinessException e){
        return R.err(R.CODE_ERR_BUSI,e.getMessage());
    }


    @ExceptionHandler(Exception.class)
    public R<?> handlesException(Exception e){
        log.error("系统错误！",e);
        return R.err(R.CODE_ERR_SYS,"系统错误！");
    }

}
